package com.wyz.saas.common.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.*
import com.atom.core.AtomApi
import com.wyz.saas.common.R


class EmptyView : LinearLayout {

    private var mEmptyImg: ImageView? = null
    private var mStatusText: TextView? = null
    private var mLoading: ProgressBar? = null

    private val mDrawableIds = intArrayOf(0, 0)
    private val mTextIds = intArrayOf(0, 0, 0)

    private var mBindViews: Array<out View>? = null

    constructor(context: Context?) : super(context) {
        init(null, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        init(attrs, defStyle)
    }

    private fun init(attrs: AttributeSet?, defStyle: Int) {
        View.inflate(context, R.layout.view_layout_empty, this)
        mEmptyImg = findViewById(R.id.im_empty)
        mStatusText = findViewById(R.id.txt_empty)
        mLoading = findViewById(R.id.loading)
        // Load attributes
        val a = context.obtainStyledAttributes(
            attrs, R.styleable.EmptyView, defStyle, 0
        )
        mDrawableIds[0] = a.getInt(R.styleable.EmptyView_comEmptyDrawable, R.drawable.status_empty)
        mDrawableIds[1] = a.getInt(R.styleable.EmptyView_comErrorDrawable, R.drawable.status_empty)
        mTextIds[0] = a.getInt(R.styleable.EmptyView_comEmptyText, R.string.prompt_empty)
        mTextIds[1] = a.getInt(R.styleable.EmptyView_comErrorText, R.string.prompt_error)
        mTextIds[2] = a.getInt(R.styleable.EmptyView_comLoadingText, R.string.prompt_loading)
        a.recycle()
    }

    /**
     * 绑定一系列数据显示的布局
     * 当前布局隐藏时（有数据时）自动显示绑定的数据布局
     * 而当数据加载时，自动显示Loading，并隐藏数据布局
     * @param views 数据显示的布局
     */
    fun bind(vararg views: View) {
        mBindViews = views
    }

    /**
     * 更改绑定布局的显示状态
     *
     * @param visible 显示的状态
     */
    private fun changeBindViewVisibility(visible: Int) {
        val views = mBindViews
        if (views == null || views.isEmpty()) return
        for (view in views) {
            view.visibility = visible
        }
    }

    fun triggerEmpty() {
        mLoading?.visibility = GONE
        mEmptyImg?.setImageResource(mDrawableIds[0])
        mEmptyImg?.visibility = VISIBLE
        mStatusText?.setText(mTextIds[0])
        visibility = VISIBLE
        changeBindViewVisibility(GONE)
    }

    fun triggerNetError() {
        mLoading?.visibility = GONE
        mEmptyImg?.setImageResource(mDrawableIds[1])
        mEmptyImg?.visibility = VISIBLE
        mStatusText?.setText(mTextIds[1])
        visibility = VISIBLE
        changeBindViewVisibility(GONE)
    }

    @SuppressLint("ShowToast")
    fun triggerError(str: String) {
        AtomApi.getInstance().toast(str)
        visibility = VISIBLE
        changeBindViewVisibility(GONE)
    }

    fun triggerLoading() {
        mEmptyImg?.visibility = GONE
        mStatusText?.setText(mTextIds[2])
        mLoading?.visibility = VISIBLE
        visibility = VISIBLE
        changeBindViewVisibility(GONE)
    }

    fun triggerOk() {
        visibility = GONE
        changeBindViewVisibility(VISIBLE)
    }

    fun triggerOkOrEmpty(isOk: Boolean) {
        if (isOk)
            triggerOk()
        else
            triggerEmpty()
    }
}